package servlet;

import dao.JDBC;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Date;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html.charset=UTF-8");
        int ID = Integer.parseInt(req.getParameter("ID"));
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String college = req.getParameter("college");
        String province = req.getParameter("province");
        String city = req.getParameter("city");
        String temperature = req.getParameter("temperature");
        String return1 = req.getParameter("return");
        String question = req.getParameter("question");
        String confirm = req.getParameter("confirm");
        Date date = new Date(System.currentTimeMillis());
        Connection connection=null;
        try{
            connection= JDBC.getConnection();
            String sql ="insert into epidemic(ID,name,sex,college,province,city,temperature,return1,question,confirm,date) values(?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement pr=connection.prepareStatement(sql);
            pr.setInt(1,ID);
            pr.setString(2,name);
            pr.setString(3,sex);
            pr.setString(4,college);
            pr.setString(5,province);
            pr.setString(6,city);
            pr.setString(7,temperature);
            pr.setString(8,return1);
            pr.setString(9,question);
            pr.setString(10,confirm);
            pr.setString(11,date.toString());
            int row=pr.executeUpdate();
            if(row!=0){
                resp.getWriter().print("添加成功");
            }else{
                resp.getWriter().print("添加失败");
            }
            pr.close();
            connection.close();
        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
            resp.getWriter().print("失败");
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
